/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.minetsh.imaging.sample;

import me.minetsh.imaging.imageData.ImagePicker;
import me.minetsh.imaging.photoview.bean.ImageItem;
import me.minetsh.imaging.photoview.ui.ImageGridAbility;
import me.minetsh.imaging.view.GlobalData;
import me.minetsh.imaging.view.IMGEditBaseAbility;
import me.minetsh.imaging.view.PhotoUtil;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.util.ArrayList;

/**
 * ImageEditSampleAbility
 *
 * @since 2021-07-27
 */
public class ImageEditSampleAbility extends Ability {
    private Image sdv_image, sdv_image_edit;
    private Button btn_choose_image;
    private ImagePicker imagePicker;
    private ArrayList<ImageItem> images = null;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_image_edit_sample);
        ResourceManager resManager = this.getResourceManager();
        try {
            int color = resManager.getElement(ResourceTable.Color_colorPrimaryDark).getColor();
            getWindow().setStatusBarColor(color);
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.fillInStackTrace();
        }

        imagePicker = ImagePicker.getInstance();
        imagePicker.setStatus(0);
        imagePicker.setMultiMode(false);

        sdv_image = (Image) findComponentById(ResourceTable.Id_sdv_image);
        sdv_image_edit = (Image) findComponentById(ResourceTable.Id_sdv_image_edit);
        btn_choose_image = (Button) findComponentById(ResourceTable.Id_btn_choose_image);

        btn_choose_image.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                imagePicker.setStatus(0);
                chooseImagesFromCustomAlbum();
            }
        });
    }
    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        try {
            super.onAbilityResult(requestCode, resultCode, resultData);
            if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
                if (resultData != null && requestCode == 100) {
                    images = (ArrayList<ImageItem>) resultData.getSerializableParam(ImagePicker.EXTRA_RESULT_ITEMS);
                    launchIMGEditAbility();
                }
            }

            if (requestCode == 101 && resultData != null) {
                sdv_image_edit.setPixelMap(GlobalData.getInstance().getResultPixelMap());
            }
        } catch (Exception e) {
            images = new ArrayList<>();
        }
    }

    private void launchIMGEditAbility() {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withBundleName(getBundleName())
                .withDeviceId("")
                .withAbilityName(IMGEditBaseAbility.class.getName())
                .build();
        intent.setOperation(operation);
        if (images.size() != 0) {
            intent.setParam(ImageGridAbility.EXTRAS_IMAGES, images.get(0).uriSchema);
            sdv_image.setPixelMap(PhotoUtil.getPixelMapByUri(getContext(), images.get(0).uriSchema));
            sdv_image_edit.setPixelMap(null);
        }
        startAbilityForResult(intent, 101);
    }

    private void chooseImagesFromCustomAlbum() {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(ImageGridAbility.class.getName())
                .build();
        intent.setOperation(operation);
        startAbilityForResult(intent, 100);
    }
}
